﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFM.API;
using CFM.DTO;
using CFM.Common;
using System.Text.RegularExpressions;

namespace CFM
{
    public partial class frmFormulaProcess : Form
    {
        public Product CurrentProduct = null;
        private FormulaProcessAPI _apiFopro=null;
        private UnitAPI _apiUnit =null;
        private ProductAPI _apiPro = null;
        private bool isEdit = true;
        //string fp = string.Empty;
        public frmFormulaProcess()
        {
            InitializeComponent();
           
        }

        private void frmFormulaProcess_Load(object sender, EventArgs e)
        {
            if (CurrentProduct != null)
            {
                this.Text ="Công thức pha chế " + CurrentProduct.ProductName;
            }
            _apiFopro = new FormulaProcessAPI();
            _apiPro = new ProductAPI();
            _apiUnit = new UnitAPI();

            EnableControl(false);
            grvFormulaProcess.AutoGenerateColumns = false;

            LoadcbxProduct();
            LoadcbxUnit();
            BindDataGridView();
        }
        void BindDataGridView()
        {
            grvFormulaProcess.DataSource = _apiFopro.GettFormulaProcessByProductId(CurrentProduct.ProductId);

        }    
        private void LoadcbxProduct()
        {
            Product product = new Product() { ProductId = "-1", ProductName = "--Chọn nguyên liệu--" };
            var products = new List<Product>();
            products.Add(product);
            products.AddRange(_apiPro.GetAll());
            cbxFormulaProcess.DataSource = products;
            cbxFormulaProcess.DisplayMember = "ProductName";
            cbxFormulaProcess.ValueMember = "ProductId";
            cbxFormulaProcess.SelectedValue = "-1";
        }
        private void LoadcbxUnit()
        {
            Unit unit = new Unit() { UnitId = "-1", UnitName = "--Chọn đơn vị--" };
            List<Unit> units = new List<Unit>();
            units.Add(unit);
            units.AddRange(_apiUnit.GetAll());            
            cbxUnit.DataSource = units;            
            cbxUnit.DisplayMember = "UnitName";
            cbxUnit.ValueMember = "UnitId";
            cbxUnit.SelectedValue = "-1";
        }

        private void grvFormulaProcess_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            FormulaProcess fomu = grvFormulaProcess.Rows[e.RowIndex].DataBoundItem as FormulaProcess;
            cbxFormulaProcess.SelectedValue = fomu.ProcessProductId.Trim();
            txtQuantity.Text = fomu.Quantity.ToString();
            cbxUnit.SelectedValue = fomu.UnitId.Trim();

            EnableControl(false);
            MethodHelper.ClearErrorProvider();

            btnEdit.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = false;
            isEdit = true;
        }
        private void EnableControl(bool flag)
        {
            txtQuantity.Enabled = flag;
            cbxUnit.Enabled = flag;
            cbxFormulaProcess.Enabled = flag;
        }
        private void ResetControl()
        {
            txtQuantity.Text = string.Empty;
            btnSave.Enabled = false;
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            cbxUnit.SelectedValue = "-1";
            cbxFormulaProcess.SelectedValue = "-1";

            MethodHelper.ClearErrorProvider();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            EnableControl(true);
            ResetControl();

            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
            btnSave.Enabled = true;
            isEdit = false;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            //if (cbxFormulaProcess.SelectedValue != null && cbxUnit.SelectedValue!=null && txtQuantity.Text.Length>0)
            //{
            EnableControl(true);
            MethodHelper.ClearErrorProvider();
            btnSave.Enabled = true;
            cbxFormulaProcess.Enabled = false;

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //string unitId = cbxUnit.SelectedValue.ToString();
            if (!CheckBeforeSave())
                return;
            FormulaProcess formu = new FormulaProcess();
            formu.ProductId = CurrentProduct.ProductId;// phai vay kook
            formu.ProcessProductId = Convert.ToString(cbxFormulaProcess.SelectedValue);
            formu.UnitId=Convert.ToString(cbxUnit.SelectedValue);
            formu.Quantity = Convert.ToInt32(txtQuantity.Text);
            if (isEdit)
            {
                _apiFopro.Update(formu);
            }
            else
            {
                _apiFopro.Create(formu);
            }
            BindDataGridView();
            ResetControl();
            MethodHelper.ClearErrorProvider();

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (cbxFormulaProcess.SelectedValue != null && cbxUnit.SelectedValue != null && txtQuantity.Text.Length > 0)
            {
                _apiFopro.Delete(CurrentProduct.ProductId, cbxFormulaProcess.SelectedValue.ToString());


                ResetControl();
                btnEdit.Enabled = false;
                btnDelete.Enabled = false;
                BindDataGridView();

            }
        }
        private bool CheckBeforeSave()
        {
            bool flag = true;
            if (txtQuantity.Enabled && txtQuantity.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtQuantity, Const.EMPTY_MESSAGE);
                flag = false;
            }
            if (cbxFormulaProcess.Enabled && cbxFormulaProcess.SelectedValue.ToString() == "-1")
            {
                MethodHelper.AddErrorProvider(cbxFormulaProcess, Const.EMPTY_MESSAGE);
                flag = false;
            }
            if (cbxUnit.Enabled && cbxUnit.SelectedValue.ToString() == "-1")
            {
                MethodHelper.AddErrorProvider(cbxUnit, Const.EMPTY_MESSAGE);
                flag = false;
            }

            return flag;
        }

        private void txtQuantity_KeyPress(object sender, KeyPressEventArgs e)
        {
            MethodHelper.OnlyNumberFloatTextBox(sender as TextBox, e);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
        
    }
}
